package com.alpaca.recommend.service;

import com.alpaca.recommend.bean.*;
import com.alpaca.recommend.util.MD5Util;
import com.alpaca.recommend.util.SpringContextLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by cnJason on 2015/2/11.
 */
@Service
public class ItemService {
    @Autowired
    URecommendService uRecommendService;

    /**
     * 添加item动作.
     *
     * @param itemId      rowkey id
     * @param tags        标签值，用逗号隔开【1,2,3】
     * @param content     内容
     * @param channelCode 频道号
     */
    public void addItem(String itemId, String[] tags, String content, String channelCode) {

        Item item = new Item();
        //初始化对象.
        item.init(itemId, tags, content, channelCode);
        //保存.
        item.save();


        for (int i = 0; i < tags.length; i++) {
            //保存tagItem关系.
            TagItem tagItem = new TagItem();
            //默认保存一个初始化的关系.
            tagItem.init(channelCode, itemId, tags[i]);
            tagItem.save();

            TagUser uTags = new TagUser();
            String rowkeyPrefix = MD5Util.md5(channelCode) + MD5Util.md5(tags[i]);
            List<TagUser> tagUserList = uTags.findByPrefix(rowkeyPrefix);

            //遍历所有用户.
            for (TagUser user : tagUserList) {
                String userId = user.getString("u", "uid");
                //将新文章插入到用户推荐列表中
                uRecommendService.setToFirst(channelCode, userId, itemId);
            }
            //将新文章至于默认推荐列表中
            uRecommendService.setToFirst(channelCode, "0", itemId);
        }
    }

    /**
     * 删除内容.
     *
     * @param itemId itemId.
     */
    public void delItem(String itemId) {
        Item item = Item.dao.get(itemId);
        if (item != null) {
            String tags = item.getString(Item.ITEMS_I_FAMILY, Item.ITEMS_I_TAG);
            String channelCode = item.getString(Item.ITEMS_I_FAMILY, Item.ITEMS_I_CHANNEL_CODE);

            String[] tagArray = tags.split(",");
            for (int i = 0; i < tagArray.length; i++) {
                String rowkey = TagItem.getRowkey(channelCode, itemId, tagArray[i]);
                TagItem tagItem = TagItem.dao.get(rowkey);

                //获取tagUser列表.
                TagUser uTags = new TagUser();
                String rowkeyPrefix = MD5Util.md5(channelCode) + MD5Util.md5(tagArray[i]);
                List<TagUser> tagUserList = uTags.findByPrefix(rowkeyPrefix);

                //遍历所有用户.
                for (TagUser user : tagUserList) {
                    String userId = user.getString("u", "uid");
                    uRecommendService.delete(channelCode, userId, itemId);
                    UItem uItem = new UItem();
                    uItem.delete(UItem.getRowKey(channelCode,userId,itemId));
                }
                //删除tagItem.
                TagItem.dao.delete(rowkey);
            }
            uRecommendService.delete(channelCode, "0", itemId);

            //删除item
            Item.dao.delete(itemId);
        }

    }


    public static void main(String[] args) {
        SpringContextLoader.init();

//        Item item = new Item();
//        item.get("2001");
//        System.out.println(item.getString(Item.ITEMS_I_FAMILY, Item.ITEMS_I_TAG));
//        System.out.println(item.getString(Item.ITEMS_I_FAMILY, Item.ITEMS_I_CHANNEL_CODE));
//        System.out.println(item.getString(Item.ITEMS_I_FAMILY, Item.ITEMS_I_CONTENT));
//        System.out.println(item.getRowKey());


//        TagItem tagItem = new TagItem();
//        tagItem.get(Bytes.toBytes(MD5Util.md5("1")+MD5Util.md5("美貌")+"2001"));
//        System.out.println(tagItem.getInt("t", "r"));
//        System.out.println(tagItem.getInt("t", "c"));
//        System.out.println(tagItem.getRowKey());

        URecommend uRecommend = new URecommend();
        uRecommend.get(MD5Util.md5("0") + MD5Util.md5("1"));
        String ids = uRecommend.getString("r", "ids");
        System.out.println(ids);
    }
}
